using System;
using System.Collections.Generic;
using DTO;
using System.Data;
using System.Data.OleDb;


namespace DAO
{
    public class PhanquyenDao:IDaoObject<PhanQuyenDto, int>
    {
 
        #region IDaoObject<PhanQuyenDto,int> Members

        public PhanQuyenDto SaveOrUpdate(PhanQuyenDto obj)
        {
            OleDbConnection cn = MyConnection.ConnectionData();
            string strSql;
            if (obj.Id == 0)
            {
                 strSql= "Insert into Phanquyen (Tenphanquyen) values ('"+obj.TenPhanQuyen+"')";
            }
            else
            {
                strSql = "Update Phanquyen set Tenphanquyen='"+obj.TenPhanQuyen+"' where ID='"+obj.Id+"'";
            }
            OleDbCommand cmd = new OleDbCommand(strSql, cn);
            //cmd.Parameters.Add("@Id", OleDbType.Integer);
            //cmd.Parameters.Add("@TenPhanQuyen", OleDbType.WChar);            
            obj.Id = cmd.ExecuteNonQuery();
            cn.Close();
            return obj;
        }

        public PhanQuyenDto SaveOrUpdate(PhanQuyenDto obj, OleDbTransaction transaction)
        {
            throw new NotImplementedException();
        }

        public void Delete(int id)
        {
            OleDbConnection cn = MyConnection.ConnectionData();
            string strSql = "Delete From Nhanvien where Nhanvien.Maphanquyen='"+id+"' Delete From Phanquyen Where Id = ? ";
            OleDbCommand cmd = new OleDbCommand(strSql, cn);
            cmd.Parameters.Add("@Id", OleDbType.Integer);
            cmd.Parameters["@Id"].Value = id;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public PhanQuyenDto Select(int id)
        {
            DataTable dt = new DataTable();
            OleDbConnection cn = MyConnection.ConnectionData();
            string strSql = "Select * From Phanquyen where ID='"+id+"'";
            OleDbDataAdapter da = new OleDbDataAdapter(strSql, cn);
            da.Fill(dt);
            cn.Close();
            PhanQuyenDto dto = new PhanQuyenDto();
            DataRow row = dt.Rows[0];
            dto.Id = Convert.ToInt32(row["Id"].ToString());
            dto.TenPhanQuyen = (string) row["Tenphanquyen"];
            return dto;
        }

        public PhanQuyenDto[] SelectAll()
        {
            //throw new NotImplementedException();
            OleDbConnection cn = MyConnection.ConnectionData();
            string strSQL = "select * from Phanquyen ";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            OleDbDataReader dr = cmd.ExecuteReader();
            List<PhanQuyenDto> ldpdto = new List<PhanQuyenDto>();
            while (dr.Read())
            {
                PhanQuyenDto ldp = new PhanQuyenDto();
                ldp.Id = Convert.ToInt32(dr["ID"].ToString());
                ldp.TenPhanQuyen = dr["Tenphanquyen"].ToString();
                ldpdto.Add(ldp);
            }
            dr.Close();
            cn.Close();
            return ldpdto.ToArray();
        }

        #endregion
    }

}